clear
set type double
set more 1

***************************************
*NOTES
*estimation at the month-firm-destination
*analogous to xcurrency
***********************************************

***************************************************************

capture log using log/AppendixTableB1_mchoice.log, replace

*1. prepare data
***************************
*market share
*only use up to 2019 in parallel with other rhs variables
use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop *X
	drop if year==2020
	rename country cty_code
	drop if missing(valueI)
    	drop if missing(currency)
 	gen hs4=substr(cncode,1,4)
    	collapse (sum) value=valueI, by(vat hs4 cty_code year)
    	merge m:1 cty_code hs4 year using ../data/M_hs4_2012_2019, keep(match) nogen
    	
	gen ms=value/totmvalue	
	drop if missing(ms)
	replace ms=1 if ms>1
	collapse (mean) ms_imp=ms, by(vat hs4 cty_code)
	drop if cty_code=="EU"
save temp/ms_imp, replace	


**********************************************************
*2. Preparing data for estimating currency use by imports
**********************************************************
**********************************************************

use ../../rawdata/update_feb21/currency_extraEU_2017_2020, clear
	drop date *X
	rename country cty_code
	drop if missing(valueI)
    	drop if missing(currency)
	drop if cty_code=="EU"
	rename valueI value

*deal with multiple currencies (10% duplicates)
	gen cat=currency=="EUR"
	collapse (sum) value, by(vat cncode cty_code year month cat)

	egen totvalue=sum(value), by(vat cncode cty_code year month)
	
	gen value_EUR=value if cat==1
	gen sh_EUR=value_EUR/totvalue
	replace  sh_EUR=0 if cat==0

*only keep euro obs when multiple and report value share
	duplicates tag vat cncode cty_code year month, gen(tag)
	drop if tag==1 & cat==0

	
** merging in relevant variables: export share, employment, import share and constructing variables **
*instead of phi, here we merge export share
********************************************************	

*merge manuf indicators
	merge m:1 vat using ../data/manuf_firm, keep(match master) nogen
			
*construct overall market share 
	gen hs4=substr(cncode,1,4)
	merge m:1 vat hs4 cty_code using temp/ms_imp, keep(match) nogen
	
	
merge m:1 vat using ../data/xshare_xeu_currency, keep(match) nogen

merge m:1 vat cty_code hs4 using ../data/mcompetitor_mcurrency, keep(match master) nogen
	gen date=ym(year, month)
	format date %tm

merge m:1 vat using temp/inFDI, keep(match master)
	gen inFDI=_merge==3
	drop _merge
	
merge m:1 vat using temp/outFDI, keep(match master)
	gen outFDI=_merge==3
	drop _merge	

	keep if manuf_firm==1	


********************************************************
*ESTIMATION
***********************************************
	merge m:1 vat using temp/memp, keep(match) nogen
	gen lnmemp=ln(memp)
	drop if missing(lnmemp)
	
*merge on instrument for currency share	
*need to merge on HS2017, because instrument is in HS2012
	
	gen hs6 = substr(cncode,1,6)
	
	rename hs6 hs2017
	merge m:1 hs2017 using ../../rawdata/UN_hs2017_to_hs2012_conversion.dta, keep(1 3) nogen
	* only 3 hs2017 codes from the original file were not in the conversion table: 993024, 993027, 993099
	rename hs2012 hs6
	
	rename hs4 hs4_2017 
	gen hs4 = substr(hs6,1,4)
		
	* Now we can merge on the file with the pegged shares
	merge m:1 hs4 cty_code using ../data/2017_2019_comtrade_US_sh_imp, keep(1 3) nogen
	rename hs4 hs4_2012
	rename hs4_2017 hs4
	gen IVsh_other= IVsh_pegged_value- IVsh_US- IVsh_CHN
		

*variables
**************************
	gen Im=1 - sh_EUR
	
save ../data/mcurrencychoice_estdata, replace		
		
	egen cp=group(hs4 cty_code)
	egen cty_num=group(cty_code)

**********************************
eststo clear

*col1 - baseline
	eststo: reghdfe Im mxshare_xeu lnmemp ms_imp, absorb(cp date) cluster(vat)

*col2 - with export share split 
	eststo: reghdfe Im mxshare_xeu_euro mxshare_xeu_other mxshare_xeu_missing lnmemp ms_imp , absorb(cp date) cluster(vat)

*col3 - with competitor and additional controls	
	eststo: reghdfe Im mxshare_xeu_euro mxshare_xeu_other mxshare_xeu_missing lnmemp ms_imp outFDI inFDI mshcompetitor_other , absorb(cty_num hs4 date) cluster(vat)

*col4 - with instruments all
	eststo: ivreghdfe Im mxshare_xeu_euro mxshare_xeu_other mxshare_xeu_missing lnmemp ms_imp outFDI inFDI (mshcompetitor_other=IVsh_US IVsh_CHN IVsh_other), absorb(cty_num hs4 date) cluster(vat) first savefirst
	local cdf = e(cdf)
	local j = e(j)
	local jp = e(jp)
	estadd scalar cdf = `cdf': _ivreg2_mshcompetitor_other
	estadd scalar j = `j': _ivreg2_mshcompetitor_other
	estadd scalar jp = `jp': _ivreg2_mshcompetitor_other


esttab using "../results/AppendixTableB1_mchoice.csv", se keep(mxshare_xeu mxshare_xeu_euro mxshare_xeu_other lnmemp ms_imp mshcompetitor_other outFDI inFDI) b(3) se(3) r2 ar2 star(* 0.10 ** 0.05 *** 0.01) order(mxshare_xeu mxshare_xeu_euro mxshare_xeu_other lnmemp ms_imp outFDI inFDI mshcompetitor_other) compress replace
esttab _ivreg2_mshcompetitor_other using "../results/AppendixTableB1_IV_first_stage_2020.csv", se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) scalars("cdf Cragg-Donald F-stat." "j Hansen J-stat." "jp p-val. of Hansen J-stat.") order(sh_US sh_CHN sh_other mxshare_xeu mxshare_xeu_euro mxshare_xeu_other lnmemp ms_imp outFDI inFDI mshcompetitor_other) compress replace

eststo clear
log close



	


